//
//  WHandleData.m
//  Go!
//
//  Created by 凌風 on 16/7/12.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "WHandleData.h"
#import "NetWorkManager+Travel.h"
#import "WShopModel.h"
#import "W_DealModel.h"

@interface WHandleData ()


@end

@implementation WHandleData


#pragma mark -- 获取商户列表
- (void)getShopsDataSourceWithSubcatId:(NSString *)subcatId {
    
    [NetWorkManager acquireShopInfoWithPage:nil subcat_ids:subcatId SuccessBlock:^(id responseObj) {
        
        if ([responseObj[@"data"][@"shops"] isKindOfClass:[NSArray class]]) {
            self.shopsArray = [NSMutableArray array];
            
            [responseObj[@"data"][@"shops"] enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                // 处理成Model
                [self handleDataWithObj:obj];
                
            }];
            
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"shopsDataSource" object:self.shopsArray userInfo:@{@"user":@"shopsTableView"}];
        }
        
        
    } FailureBlock:^(NSError *error) {
        
    }];
 
}


- (void)handleDataWithObj:(NSDictionary *)obj {
    NSMutableDictionary *handledDict = [NSMutableDictionary dictionary];
    [handledDict setObject:obj[@"shop_id"] forKey:@"shop_id"];
    [handledDict setObject:obj[@"shop_name"] forKey:@"shop_name"];
        
    __block CGFloat minPrice = 0;
    __block NSUInteger minIndex = 0;
        
    if ([obj[@"deals"] isKindOfClass:[NSArray class]]) {
        [obj[@"deals"] enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull deal, NSUInteger idx, BOOL * _Nonnull stop) {
                
            if (idx == 0) {
                minPrice = [deal[@"current_price"] floatValue];
                minIndex = 0;
            } else {
                
                if (minPrice > [deal[@"current_price"]floatValue]) {
                    minPrice = [deal[@"current_price"] floatValue];
                    minIndex = idx;
                }
            }
        }];
    }
        
    [handledDict setObject:[NSString stringWithFormat:@"￥%.2lf", minPrice / 100] forKey:@"price"];
        
    [handledDict setObject:obj[@"deals"][minIndex][@"image"] forKey:@"imageUrl"];

    WShopModel *model = [[WShopModel alloc] init];
    [model buildModelWithDict:handledDict];
    
    [self.shopsArray addObject:model];
    
}


#pragma mark -- 获取商户详情
- (void)getShopDetailInfoWithShopID:(NSString *)shopId {
    
    [NetWorkManager acquireShopDetailInfoWithShopID:shopId SuccessBlock:^(id responseObj) {
        
//        NSLog(@"-->%@", responseObj[@"shop"]);
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shopsDataSource" object:responseObj[@"shop"] userInfo:@{@"user":@"shopDetailInfo"}];
        
    } FailureBlock:^(NSError *error) {
        
    }];
}

#pragma mark -- 获取商户下团单列表
- (void)getShopDealsWithShopID:(NSString *)shopId {
    [NetWorkManager acquireShopDealsWithShopID:shopId SuccessBlock:^(id responseObj) {
        
//        NSLog(@"-->%@", responseObj);
        
        if ([responseObj[@"deals"] isKindOfClass:[NSArray class]]) {
            
            self.dealsArray = [NSMutableArray array];
            
            [responseObj[@"deals"] enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [self handleDealsWithObj:obj];
                
//                NSLog(@"****%@", obj[@"description"]);
                
            }];
            
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"DetailVcShopDeals" object:self.dealsArray userInfo:@{@"user":@"shopDeals"}];
        
//        NSLog(@"%@", self.dealsArray);
        
    } FailureBlock:^(NSError *error) {
        
    }];
}

- (void)handleDealsWithObj:(NSDictionary *)obj {
    NSMutableDictionary *handledDict = [NSMutableDictionary dictionary];
    [handledDict setObject:obj[@"deal_id"] forKey:@"deal_id"];
    [handledDict setObject:obj[@"deal_murl"] forKey:@"deal_murl"];
    [handledDict setObject:[NSString stringWithFormat:@"简介:%@",obj[@"description"]] forKey:@"description"];
    [handledDict setObject:obj[@"tiny_image"] forKey:@"tiny_image"];
    [handledDict setObject:[NSString stringWithFormat:@"市场价:%.2lf", [obj[@"market_price"] floatValue] / 100] forKey:@"market_price"];
    [handledDict setObject:[NSString stringWithFormat:@"促销价:%.2lf", [obj[@"promotion_price"] floatValue] / 100] forKey:@"promotion_price"];
    [handledDict setObject:obj[@"min_title"] forKey:@"min_title"];
    [handledDict setObject:[NSString stringWithFormat:@"已售出%@", obj[@"sale_num"]] forKey:@"sale_num"];
    
    W_DealModel *model = [[W_DealModel alloc] init];
    [model buildModelWithDict:handledDict];
    
    [self.dealsArray addObject:model];
    
}


@end
